<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\facade\Session;
use think\Request;
use think\facade\View;
use think\exception\ValidateException;

use app\admin\validate\BannerValidate;

use app\admin\model\Banner;

class BannerController extends BaseController
{
    // 渲染轮播图列表页面
    public function banner_list()
    {   
        return view();
    }

    // 渲染轮播图添加页面
    public function banner_add()
    {   
        return view();
    }

    // 渲染轮播图编辑页面
    public function banner_edit()
    {   
        $data = input('get.');
        $bannerModel = new Banner();

        $bannerInfo = $bannerModel->getFind(['id' => $data['id']])['data'];

        View::assign('info', $bannerInfo);
        return view();
    }

    // 获取轮播图列表
    public function banner_data()
    {   
        $bannerModel = new Banner();
        $data = input('get.');

        // 验证数据
        try{
            validate(BannerValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $count = $bannerModel->getNum()['data'];

        $field = 'id, title, img, sort, is_show';
        $list = $bannerModel->getList([], 1, $data['page'], $data['limit'], $field, 'sort DESC, id DESC');

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        $this->success_back('ok', $list['data'], $count);
    }

    // 轮播图的显示||隐藏
    public function setIsShow()
    {
        $id = input('post.id');
        $bannerModel = new Banner();
        $isShow = $bannerModel->setIsShow($id);

        $this->success_back($isShow['msg']);
    }

    // 轮播图添加
    public function bannerAdd()
    {   
        $bannerModel = new Banner();
        $data = input('post.');
        $data['img'] = request()->file('img');

        // 验证数据
        try{
            validate(BannerValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $data['img'] = fileUp($data['img'], 'banner');
        $flag = $bannerModel->add($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('添加成功');
    }

    // 轮播图信息修改
    public function setBannerEdit()
    {   
        $bannerModel = new Banner();
        $data = input('post.');
        $data['img'] = request()->file('img');

        if(!$data['img']){
            $field = 'edit';
        }else{
            $field = 'editImg';
        }

        // 验证数据
        try{
            validate(BannerValidate::class)->scene($field)->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        if(!$data['img']){
            unset($data['img']);
        }else{
            $data['img'] = fileUp($data['img'], 'banner');
        }
        
        $flag = $bannerModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('修改成功');
    }

    // 删除轮播图
    public function bannerDel()
    {
        $bannerModel = new Banner();
        $data = input('post.');

        // 验证数据
        try{
            validate(BannerValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $flag = $bannerModel->del(['id' => $data['id']]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('删除成功');
    }
}
